package top.igotcha.eduservice.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import top.igotcha.eduservice.entity.EduChapter;
import top.igotcha.eduservice.entity.vo.ChapterVo;
import top.igotcha.eduservice.service.EduChapterService;
import top.igotcha.commonutils.utils.R;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Gotcha
 * @since 2021-08-06
 */
@RestController
@RequestMapping("/eduservice/chapter")
//@CrossOrigin
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;


    @GetMapping("getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list = chapterService.getChapterVideoByCourseId(courseId);
        return R.OK().data("allChapterVideo",list);
    }

    @PutMapping()
    public R addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return R.OK();
    }

    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        boolean flag = chapterService.deleteChapter(chapterId);
        if(flag){
            return R.OK();
        } else {
            return R.ERROR();
        }
    }

    @PostMapping()
    public R updateChapter(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);
        return R.OK();
    }

    @GetMapping("{chapterId}")
    public R getChapter(@PathVariable String chapterId){
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.OK().data("chapter",eduChapter);
    }

}

